package com.dao.Impl;

import com.dao.PersonDao;
import com.pojo.Person;
import com.util.JDBC;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class PersonDaoImpl implements PersonDao {
    @Override
    public List<Person> queryPerson() {
        String sql = "SELECT * FROM person WHERE id > ?";
        Object[] objects = new Object[1];
        objects[0] = 0;
        ResultSet resultSet = JDBC.query(sql,objects);
        List<Person> personList = new ArrayList<Person>();
        try{
            while (resultSet.next()){
                Person person = new Person();
                person.setId(resultSet.getInt(1));
                person.setName(resultSet.getString(2));
                person.setSex(resultSet.getString(3));
                person.setAge(resultSet.getInt(4));
                person.setRank(resultSet.getString(5));
                person.setDepartment(resultSet.getString(6));
                personList.add(person);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return personList;
    }

    @Override
    public int addPerson(Person person) {
        String sql = "INSERT INTO information.person (name, sex, age, `rank`, department) \n" +
                "    VALUE(?,?,?,?,?);";
        Object[] objects = new Object[5];
        objects[0] = person.getName();
        objects[1] = person.getSex();
        objects[2] = person.getAge();
        objects[3] = person.getRank();
        objects[4] = person.getRank();
        int num = 0;
        num = JDBC.alter(sql,objects);
        return num;
    }

    @Override
    public int delPerson(Person person) {
        String sql = "DELETE FROM person WHERE id = ?";
        Object[] objects = new Object[1];
        objects[0] = person.getId();;
        int num = 0;
        num = JDBC.alter(sql,objects);
        return num;
    }
}
